package com.ruandi.test05;

import com.ruandi.test05.bean.Book;

public class TestBook {
    public static void main(String[] args) {
        Book book1 = new Book(1, "帅", "帅", 66.6, 77, 50);
        Book book2 = new Book(2, "的", "的", 77.7, 90, 50);
        Book book3 = new Book(3, "一", "一", 88.8, 66, 50);
        Book book4 = new Book(4, "二", "二", 99.9, 99, 50);
        Book book5 = new Book(5, "三", "三", 100, 100, 50);

        //定义数组存储
        Book[] books = {book1, book2, book3, book4, book5};

        //按销量排序前
        System.out.println("----------------按销量排序前----------------");
        for (Book book : books) {
            System.out.println("编号：" + book.getId() + " 书名:" + book.getTitle() + " 作者:" + book.getAuthor() + " 价格:" + book.getPrice() + " 销量" + book.getSales() + " 库存" + book.getStock());
        }
        //循环冒泡排序取顺序
//        for (int i = 0; i < books.length - 1; i++) {
//            //定义最值索引
//            int maxIndex = i;
//            for (int j = i + 1; j < books.length; j++) {
//                if (books[j].getSales() > books[maxIndex].getSales()) {
//                    maxIndex = j;
//                }
//            }
//            //定义交换
//            Book temp = books[i];
//            books[i] = books[maxIndex];
//            books[maxIndex] = temp;
//        }
        //外层控制轮次
        for (int i = 0; i < books.length - 1; i++) {
            //内层控制交换
            for (int j = 0; j < books.length - 1 - i; j++) {
                if (books[j].getSales() < books[j + 1].getSales()) {
                    Book temp = books[j+1];
                    books[j+1] = books[j];
                    books[j] = temp;
                }

            }
        }
        System.out.println("------------------------------------------");
        System.out.println("----------------按销量排序后----------------");
        for (Book book : books) {
            System.out.println("编号：" + book.getId() +
                    " 书名:" + book.getTitle() +
                    " 作者:" + book.getAuthor() +
                    " 价格:" + book.getPrice() +
                    " 销量" + book.getSales() +
                    " 库存" + book.getStock());
        }
        System.out.println("------------------------------------------");

    }

}
